using Extentions;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Net.Http.Headers;
using System.Text;
using TesseractOCR;
using TesseractOCR.Enums;
using static TestTesseractOCR.CertSearchHelper;

namespace TestTesseractOCR.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class HomeController : ControllerBase
    {
        //private readonly ILogger<HomeController> _logger;

        //public HomeController(ILogger<HomeController> logger)
        //{
        //    _logger = logger;
        //}

        private static Engine _engine;

        public static Engine Engine
        {
            get
            {
                if (_engine != null) return _engine;
                _engine = new Engine(@"./TesseractOCR", TesseractOCR.Enums.Language.English, EngineMode.Default);
                return _engine;
            }
        }

        [HttpGet(Name = "GetTest1")]
        public string GetTest1()
        {
            string base64 = "/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAA8AKADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2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5W/wD3y3+NL/wld9/zytv++W/xo9tEPbROzFPFcV/wlt//AM8bb/vlv8aX/hL9Q/5423/fLf8AxVHtoh7aJ2wp4rh/+Ew1D/nja/8AfLf/ABVL/wAJlqP/ADxtf++W/wDiqPbRD20TuhTxXB/8JnqP/PG1/wC+G/8AiqX/AITXUv8Anhaf98N/8VR7aIe2id6KeK4D/hNtS/54Wn/fDf8AxVL/AMJxqf8AzwtP++G/+Ko9tEPbRPQRVXV/+QHqH/XtJ/6Ca4r/AITnU/8Anhaf98N/8VUdz4z1G6tZrd4bUJKjIxVWyARjj5qmVaNmKVWNmf/Z";
            byte[] bytes = Convert.FromBase64String(base64);
            var img = TesseractOCR.Pix.Image.LoadFromMemory(bytes);
            using var page = Engine.Process(img);
            Console.WriteLine("Mean confidence: {0}", page.MeanConfidence);
            Console.WriteLine("Text: \r\n{0}", page.Text.Trim());
            string fileName = Base64Helper.SaveFileFromBase642(CertSearchHelper.Base64Preff + base64, $"{page.Text.Trim()}_{page.MeanConfidence.ToString("0.00")}.gif", null);
            string fileFullName = Base64Helper.CurrentDirectory + fileName;
            Console.WriteLine(fileFullName);
            return "Get执行完成";
        }

        [HttpGet(Name = "GetCode")]
        public AjaxResult<UuidCode> GetCode()
        {
            return CertSearchHelper.GetCode(true);
        }

        [HttpGet(Name = "Get")]
        public AjaxResult<string> Get()
        {
            return CertSearchHelper.GetInfo("周扬程", "513821198903044915");
        }
    }
}
